package com.xb.xbpm.controller;

import com.xb.xbpm.bean.User;
import com.xb.xbpm.service.ILoginService;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 登陆
 * @Author gxd
 * @Date： 2018/7/23
 */
@Controller
@RequestMapping(value = "login")
public class LoginController {

    @Autowired
    ILoginService loginService;

    /**
     * 注册
     */
    @RequestMapping("/register")
    public @ResponseBody
    Map<String, Object> register(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        Integer r=0;
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String contact=request.getParameter("contact");
        String tel=request.getParameter("tel");
        String email=request.getParameter("email");

        User user =new User();
        user.setUsername(username);
        user.setToken(username);
        user.setPassword(password);
        user.setContact(contact);
        user.setEmail(email);
        user.setTel(tel);

        try {
            r=loginService.saveUser(user);
        } catch (Exception e) {

        }
        map.put("result",r);
        return map;
    }

    /**
     *
     * 返回用户信息
     */
    @RequestMapping("/getUserInfo")
    public @ResponseBody
    Map<String, Object> getUserInfo(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        Integer r=-1;
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        User user =new User();
        user.setUsername(username);

        if(Tools.notEmpty(password)){
            user.setPassword(password);
        }
        User user1=loginService.getUserInfo(user);
        if(user1!=null){
            r=1;
        }
        map.put("result",r);
        map.put("userInfo",user1);
        return map;
    }


    /**
     * 验证登陆
     */
    @RequestMapping("/checkLogin")
    public @ResponseBody
    Map<String, Object> checkLogin(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        Integer r=-1;
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        if(Tools.isEmpty(username)|| Tools.isEmpty(password)){
            map.put("result",r);
            return map;
        }

        User user =new User();
        user.setUsername(username);
        user.setPassword(password);

        User user1=loginService.getUserInfo(user);
        if(user1==null){
            map.put("result",r);//用户名密码错误
            return map;
        }
        r=1;
        map.put("result",r);
        return map;
    }






}
